Structs

  • Syntax:

    struct type_name {
        field_type1 field_name1;
        field_type2 field_name2;
        field_type3 field_name3;
        .
        .
    } variables_with_this_type;
    
    • type_name :

      • A name for the structure type.

    • variables_with_this_type :

      • Optional field.

      • Can be a set of valid identifiers for objects that have the type of this structure.

  • Defining a struct type, and creating 3 variables with its type separately:

    struct product {
        int weight;
        double price;
    };
    
    product apple, banana, melon;
    
  • Defining a struct type, and creating 3 variables with its type right away:

    struct product {
        int weight;
        double price;
    } apple, banana, melon;
    
    • Also works:

    struct {
        int weight;
        double price;
    } apple, banana, melon;
    
  • Pointer to a struct:

    struct movie_t {
        string title;
        int year;
    };
    
    movie_t  my_movie;
    movie_t* p_movie;
    
    • Dereferencing a struct to get its value:

      (*p_movie).
      
      p_movie->